<template>
  <div class="app">
    <div class="dafaaf">
      <div class="heads">
        <img src="@/assets/1.png" alt="">
      </div>
      <div class="aaffafff">
         <div>尝新</div>
         <div>尝热</div>
         <div>尝潮流</div>
      </div>
    </div>
    <div class="register-wrapper">
      <div id="register">
        <p class="title">会员注册</p>
        <el-form
          :model="ruleForm2"
          status-icon
          :rules="rules2"
          ref="ruleForm2"
          label-width="0"
          class="demo-ruleForm"
        > 
          <el-form-item prop="tel">
            <el-input size="small" v-model="ruleForm2.tel" placeholder="请输入手机号" blur="numberVerify()"></el-input>
          </el-form-item>
          <el-form-item prop="smscode" class="code">
            <el-input size="small" v-model="ruleForm2.smscode" placeholder="验证码"></el-input>
            <el-button size="small" type="primary" :disabled='isDisabled' @click="sendCode">{{buttonText}}</el-button>
          </el-form-item>
           <el-form-item>
            <el-input size="small" v-model="ruleForm2.recommend" auto-complete="off" placeholder="输入推荐码"></el-input>
          </el-form-item>
          <el-form-item prop="pass">
            <el-input size="small" type="password" v-model="ruleForm2.pass" auto-complete="off" placeholder="输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input size="small" type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="ruleForm2.consent" style="float:left;line-height: 20px;">我已阅读并接受以下条款<span style="color:blue">《云吞象合作协议》</span></el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="submitForm('ruleForm2')" style="width:100%;">注册</el-button>
            <p class="login" @click="gotoLogin">已有账号？立即登录</p>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import {wcode,register} from '@/apiUrl/busslogin'
export default {
  name: "Register",
  data() {
    // <!--验证手机号是否合法-->
    let checkTel = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号码'))
      } else if (!this.checkMobile(value)) {
        callback(new Error('手机号码不合法'))
      } else {
        callback()
        this.PtTelVerify()
      }
    }
    //  <!--验证码是否为空-->
    let checkSmscode = (rule, value, callback) => {
      let _th = this;
      if (value === '') {
        callback(new Error('请输入手机验证码'))
      } else {
        callback()
        console.log(value)
      }
      if(this.checkCode(value)){
        this.PtNumberVerify()
        console.log("输入完成")
      }
    }
    // <!--验证密码-->
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"))
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback()
      }
    }
    // <!--二次验证密码-->
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        pass: "",
        checkPass: "",
        tel: "",
        smscode: "",
        recommend:"",//推荐码
        consent:false,
      },
      hpCode:"",
      rules2: {
        pass: [{ validator: validatePass, trigger: 'change' }],
        checkPass: [{ validator: validatePass2, trigger: 'change' }],
        tel: [{ validator: checkTel, trigger: 'change' }],
        smscode: [{ validator: checkSmscode, trigger: 'change' }],
      },
      buttonText: '发送验证码',
      isDisabled: false, // 是否禁止点击发送验证码按钮
      flag: true
    }
  },
  methods: {
    // 手机号是否注册验证
    async PtTelVerify (){
      
    },
    //短信验证手机号
    async PtNumberVerify (){
     
    },

    // <!--发送验证码-->
    sendCode () {
      let _th =this;
      let tel = this.ruleForm2.tel
      if (this.checkMobile(tel)) {
        console.log(tel)
        let time = 60
        this.buttonText = '已发送'
        this.isDisabled = true
        var datas={
          mobile:tel
        }
        wcode(datas).then(res=>{
          console.log(res)
        }).catch(err=>{
          console.log(err)
        })
        if (this.flag) {
          this.flag = false;
          let timer = setInterval(() => {
            time--;
            this.buttonText = time + ' 秒'
            if (time === 0) {
              clearInterval(timer);
              this.buttonText = '重新获取'
              this.isDisabled = false
              this.flag = true;
            }
          }, 1000)
        }
      };

    },

    // <!--提交注册-->
    submitForm(formName) {
      if(this.ruleForm2.consent==false){
        this.$message.error('请先勾选合作协议');
      }else{
        if(this.ruleForm2.tel!=""&&this.ruleForm2.pass!=""&&this.ruleForm2.smscode!=""){

        
        var data ={
          mobile : this.ruleForm2.tel,
          password : this.ruleForm2.pass,
          code:this.ruleForm2.smscode
        }
        this.$refs[formName].validate(valid => {
          console.log(valid)
          if (valid) {
            var that=this
            register(data).then(res=>{
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
              that.$router.push({
                path: "/merchantSignIn"
              });
            }).catch(err=>{
              this.$message({
          message: '登录失败，请重新登录',
          type: 'warning'
        });
            })
          } else {
            console.log("error submit!!");
            return false;
          }
        })
        }else{
          this.$message({
          message: '手机号，密码，验证码不能为空',
          type: 'warning'
        });
        }
      } 
    },
    // <!--进入登录页-->
    gotoLogin() {
      this.$router.push({
        path: "/merchantSignIn"
      });
    },
    // 验证手机号
    checkMobile(str) {
      let re = /^1\d{10}$/
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    },
    // 验证码
    checkCode(str) {
      let re = /^\d{6}$/
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    }
  }
};
</script>

<style scoped>
.app{
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;

}
.dafaaf{
  width: 100%;
  height: 108px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
}
.heads {
  width: 239px;
  height: 100%;
  margin-left: 109px;
}
.heads img{
  width: 100%;
  margin-top: 24px;
}
.aaffafff {
  width: 223px;
  display: flex;
  justify-content: space-between;
  margin-right: 200px;
}
.aaffafff div {
  font-size: 24px;
  line-height: 100px;
  color: #EC8741;
  margin-top: 20px;
}

.register-wrapper{
  width: 100%;
  position: absolute;
  top: 108px;
  left: 0px;
  bottom: 0px;
  background: url(../assets/loginBg.png) no-repeat center center;
  background-size: 100% 100%;
}
#register {
    max-width: 340px;
    background: #fff;
    padding: 20px 40px;
    border-radius: 10px;
    position: absolute;
    z-index: 9;
    top: 128px;
    right: 150px;
}
.title {
  font-size: 26px;
  line-height: 50px;
  font-weight: bold;
  margin: 10px;
  text-align: center;
}
.el-form-item {
  text-align: center;
}
.login {
  margin-top: 10px;
  font-size: 14px;
  line-height: 22px;
  color: #1ab2ff;
  cursor: pointer;
  text-align: left;
  text-indent: 8px;
  width: 160px;
}
.login:hover {
  color: #2c2fd6;
}
.code >>> .el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.code button {
  margin-left: 20px;
  width: 140px;
  text-align: center;
}
.el-button--primary:focus {
  background: #409EFF;
  border-color: #409EFF;
  color: #fff;
}
.el-form-item {
    margin-bottom: 15px;
}
</style>
